package net.oschina.android.app;

import java.util.Properties;

import net.oschina.android.api.ApiClient;
import net.oschina.android.model.Result;
import net.oschina.android.model.User;
import net.oschina.android.util.CyptoUtils;
import net.oschina.android.util.StringUtil;
import net.oschina.android.util.UIHelper;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author HuangWenwei
 *
 * @date 2014年8月24日
 */
public class AppContext extends Application {
	
	private int loginUid = 0;	//登录用户的id
	private boolean login = false;
	
	
	@Override
	public void onCreate() {
		//Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
	}

	/**
	 * 获取APP安装包信息
	 *2014年8月26日
	 *@return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}
	
	public String getProperty(String key){
		String res = AppConfig.getAppConfig(this).get(key);
		return res;
	}
	
	public void setProperties(Properties ps){
		AppConfig.getAppConfig(this).set(ps);
	}
	
	public void saveAccountInfo(String email,String pwd){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor ed = sp.edit();
		ed.putString("username", email);
		ed.putString("password", pwd);
		ed.commit();
	}
	
	public void setProperty(String key,String value){
		AppConfig.getAppConfig(this).set(key,value);
	}
	
	public User loginVerify(String account,String pwd, boolean isRememberInfo) throws AppException{
		User user = ApiClient.login(this, account, pwd);
		if(null!=user){
			user.setAccount(account);
			user.setPwd(pwd);
			user.setRememberMe(isRememberInfo);
			Result res = user.getValidate();
			if(res.OK()){
				saveLoginInfo(user);
			}else{
				if(AppUtil.isUIThread())
					UIHelper.ToastMessage(this, "邮箱或者密码错误");
			}
				
		}
		return user;
			
	}
	
	public boolean isHttpsLogin(){
		String pref_httpslogin = getProperty(AppConfig.CONF_HTTPS_LOGIN);
		if(TextUtils.isEmpty(pref_httpslogin))
			return false;
		else
			return StringUtil.toBool(pref_httpslogin);
	}
	
	private void saveLoginInfo(final User user){
		if(null == user)
			return;
		this.loginUid = user.getUid();
		this.login = true;
		
		setProperties(new Properties(){
			{
				setProperty("user.uid", String.valueOf(user.getUid()));
				setProperty("user.name", user.getName());
				setProperty("user.face", String.valueOf(user.getFace()));
				setProperty("user.account", user.getAccount());
				setProperty("user.pwd", CyptoUtils.encode("oachinaApp", user.getPwd()));
				setProperty("user.location", user.getLocation());
				setProperty("user.followers", String.valueOf(user.getFollowers()));
				setProperty("user.fans", String.valueOf(user.getFans()));
				setProperty("user.score", String.valueOf(user.getScore()));
				setProperty("user.isRememberMe", String.valueOf(user.isRememberMe()));
			}
		});
	}
	
	
}
